//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Reporting;
using CA = ComponentArt.Web.UI;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;

namespace CommunityServer.ControlPanel.Tools.Reports
{
	/// <summary>
	/// Summary description for BlogActivityReport.
	/// </summary>
	public class ForumPostViewsReport : BaseToolsPage
	{
		#region Members

		protected CA.Grid Grid1;
		protected DropDownList PageList;
		protected ResourceLinkButton FilterButton;
		protected Modal Modal1;
		protected ActivityQueryControl UserActivityQueryControl1;
		string BegReportDate;
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.ControlPanel.Controls.ResourceControl RegionTitle;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.ControlPanel.Controls.ResourceControl UniqueCount;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		string EndReportDate;

		protected CommunityServer.Controls.StatusMessage Status;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			if (Telligent.Registration.CommunityServer.IsStandard)
			{
				Grid1.PageIndexChanged += new CA.Grid.PageIndexChangedEventHandler(OnPageIndexChanged);
				Grid1.NeedRebind += new CA.Grid.NeedRebindEventHandler(OnNeedRebind);
				Grid1.NeedDataSource += new CA.Grid.NeedDataSourceEventHandler(OnNeedDataSource);
				Grid1.ShowFooter = false;

				this.Load += new EventHandler(this.Page_Load);
			}
			else
			{
				Status.Success = false;
				Status.IsLicenseMessage = true;
				Status.Text = CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_Tools_UserActivityReport_LicenseNotAvailable");
				Status.Visible = true;
			}

			//Taken from the BaseGridControl Helper function - In the future this page should inherit from that base class
			foreach(CA.GridColumn gc in Grid1.Levels[0].Columns)
			{
				if(gc.Visible)
				{
					gc.SortedDataCellCssClass = "SortedDataCell";
					//Allows for mock resource manager calls in heading text
					//eg HeadingText="ResourceManager.CP_Photos_GridCol_Name"
					if(gc.HeadingText.StartsWith("ResourceManager."))
						gc.HeadingText = ResourceManager.GetString(gc.HeadingText.Replace("ResourceManager.",""));
				}
			}

			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack && !this.IsCallBack)
			{
				CSContext context = CSContext.Current;
				if(context.QueryString["csv"] != null && context.QueryString["csv"].ToString() == "1")
					BuildExcelDoc();
				else
					Bind();
			}
		}

		private void Bind()
		{
			ForumPostViewResultSet fpvrs = BuildReportData();
			Grid1.DataSource = fpvrs.Records;
			Grid1.DataBind();
			Grid1.RecordCount = 25;
		}

		private ForumPostViewResultSet BuildReportData()
		{
			CSContext context = CSContext.Current;
			BegReportDate = "1/1/1900";
			EndReportDate = "1/1/1900";
			Grid1.PageSize = 25;

			if (Globals.IsDate(context.QueryString["brd"]))
			{
				BegReportDate = context.QueryString["brd"];
				UserActivityQueryControl1.BegSearchReportDatePicker = DateTime.Parse(BegReportDate);
				UserActivityQueryControl1.BegSearchReportDateCalendar = DateTime.Parse(BegReportDate);
			}
			if (Globals.IsDate(context.QueryString["erd"]))
			{
				EndReportDate = context.QueryString["erd"];
				UserActivityQueryControl1.EndSearchReportDatePicker = DateTime.Parse(EndReportDate);
				UserActivityQueryControl1.BegSearchReportDateCalendar = DateTime.Parse(EndReportDate);
			}

			ForumPostViewResultSet fpvrs = new ForumPostViewResultSet();
			
			fpvrs = ReportingSqlDataProvider.GetForumPostViewResults(BegReportDate, EndReportDate);

			return fpvrs;
		}

		#region Events
		public void OnPageIndexChanged(object sender, CA.GridPageIndexChangedEventArgs args)
		{
			Grid1.CurrentPageIndex = args.NewIndex;
		}

		public void OnNeedDataSource(object sender, EventArgs oArgs)
		{
			ForumPostViewResultSet fpvrs = BuildReportData();
			Grid1.DataSource = fpvrs.Records;
		}

		public void OnNeedRebind(object sender, EventArgs oArgs)
		{
			Grid1.DataBind();
			Grid1.RecordCount = 25;
		}

		#endregion

		private void InitializeComponent()
		{
		
		}

		private void BuildExcelDoc()
		{
			CSContext context = CSContext.Current;
			BegReportDate = "1/1/1900";
			EndReportDate = "1/1/1900";
			if (Globals.IsDate(context.QueryString["brd"]))
				BegReportDate = context.QueryString["brd"];
			if (Globals.IsDate(context.QueryString["erd"]))
				EndReportDate = context.QueryString["erd"];
			string CSV = "";
			CSV = ReportingSqlDataProvider.GetForumPostViewResultsCSV(BegReportDate, EndReportDate);
			Response.ContentType = "Application/x-msexcel";
			Response.AddHeader("content-disposition", "attachment; filename=\"ForumPostViewsReport.csv\"");
			Response.Write(CSV);
			Response.End();
		}
	}
}
